package com.iotechn.unimall.app.api.tree;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.app.config.ezviz.EzvizService;
import com.iotechn.unimall.core.annotation.HttpMethod;
import com.iotechn.unimall.core.annotation.HttpParam;
import com.iotechn.unimall.core.annotation.HttpParamType;
import com.iotechn.unimall.core.annotation.param.NotNull;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.EnterpriseImageDO;
import com.iotechn.unimall.data.domain.TreeCategoryDO;
import com.iotechn.unimall.data.domain.TreeDO;
import com.iotechn.unimall.data.domain.TreeDetailsDO;
import com.iotechn.unimall.data.mapper.TreeCategoryMapper;
import com.iotechn.unimall.data.mapper.TreeDetailsMapper;
import com.iotechn.unimall.data.mapper.TreeMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TreeServiceImpl implements TreeService {

    @Autowired
    private TreeMapper treeMapper;
    @Autowired
    private TreeCategoryMapper treeCategoryMapper;
    @Autowired
    private TreeDetailsMapper treeDetailsMapper;
    @Autowired
    private CacheComponent cacheComponent;
    @Autowired
    private EzvizService ezvizService;

    @Override
    public Page<TreeDO> list(Integer page, Integer limit, String title, String categoryId) {
        List<TreeDO> TreeDO = treeMapper.selectListByFront(categoryId,(page - 1) * limit, limit);
        Integer count = treeMapper.selectCount(new EntityWrapper<>());
        return new Page<>(TreeDO, page, limit, count);
    }

    @Override
    public TreeDO detail(Integer id){
        TreeDO treeDO = treeMapper.selectByTreeId(id);
        String accessToken = cacheComponent.getRaw(EzvizService.ACCESS_TOKEN);
        if(StringUtils.isBlank(accessToken)) {
            synchronized (EzvizService.ACCESS_TOKEN){
                if(StringUtils.isBlank(accessToken))
                    ezvizService.refresh();
            }
            accessToken = cacheComponent.getRaw(EzvizService.ACCESS_TOKEN);
        }
        treeDO.setAccessToken(accessToken);
        Wrapper<TreeDetailsDO> wrapper = new EntityWrapper<TreeDetailsDO>()
                .setSqlSelect("MIN(nums) AS nums", "MIN(price) AS price")
                .eq("tree_id",id);
        List<TreeDetailsDO> list = treeDetailsMapper.selectList(wrapper);
        if(list==null || list.get(0) == null){
            treeDO.setLeastNums(0);
            treeDO.setLowPrice(0);
        }else{
            treeDO.setLeastNums( list.get(0).getNums());
            treeDO.setLowPrice(list.get(0).getPrice());
        }
        return treeDO;
    }

    @Override
    public List<TreeCategoryDO> categoryList(){
        return treeCategoryMapper.selectPullDown();
    }


    @Override
    public Page<TreeDetailsDO> detailListByTreeId(Integer page, Integer limit, String title, Long treeId) {
        Wrapper<TreeDetailsDO> wrapper = new EntityWrapper<TreeDetailsDO>()
                .eq("tree_id",treeId);
        wrapper.orderBy("id", false);
        Integer count = treeDetailsMapper.selectCount(wrapper);
        List<TreeDetailsDO> treeDetailsDO = treeDetailsMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(treeDetailsDO, page, limit, count);
    }


    @Override
    public TreeDetailsDO treeDetail(Long id) throws ServiceException{
        TreeDetailsDO treeDetailsDO = treeDetailsMapper.selectById(id);
        String accessToken = cacheComponent.getRaw(EzvizService.ACCESS_TOKEN);
        if(StringUtils.isBlank(accessToken)) {
            synchronized (EzvizService.ACCESS_TOKEN){
                if(StringUtils.isBlank(accessToken))
                    ezvizService.refresh();
            }
            accessToken = cacheComponent.getRaw(EzvizService.ACCESS_TOKEN);
        }
        TreeDO treeDO=treeMapper.selectById(treeDetailsDO.getTreeId());
        treeDetailsDO.setTreeDO(treeDO);
        treeDetailsDO.setAccessToken(accessToken);

        return treeDetailsDO;
    }

}
